
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUserInfo, getUserAvatar } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userinfo: {}
  },
  mutations: {
    updataToken(state, token) {
      state.token = token
      setToken(token)
    },
    deleteToken(state) {
      state.token = ''
      removeToken()
    },
    updataUserInfo(state, info) {
      state.userinfo = info
    },
    deleteUserInfo(state) {
      state.userinfo = {}
    }

  },
  actions: {
    async getUsinfo(content) {
      try {
        const res = await getUserInfo()
        // content.commit('updataUserInfo', res.data)
        // console.log(res)
        // 通过请求得到的用户id再请求用户的头像 并且合并两个请求对象
        const img = await getUserAvatar(res.data.userId)

        const acObj = { ...res.data, ...img.data }
        content.commit('updataUserInfo', acObj)
      } catch (error) {
        console.log(error)
      }
    }
  }
}

